// mongoose setup
require('../../../config');

var Promise = require('promise'),
  assert = require('assert'),
  mongoose = require('mongoose'),
  register = require('../../../../app/components/backend/service/register'),
  weixinDao = require('../../../../app/components/backend/db/daos/weixinuser'),
  nock = require('nock'),
  weixinModel = require('../../../../app/components/backend/db/models/weixinuser'),
  accessTokenModel = require('../../../../app/components/backend/db/models/accesstoken'),
  util = require('../../../../app/components/backend/util'),
  config = require('../../../../app/components/backend/util').config,
  access_token = 'BYEsg8wnKAM0d_7uxOjq7E7LgTeCPBw-FQa_-NVUviAMjMSKFD-85Z7rfrJa9vLX4-g3IpDJeKh6EpAp231gofjGokYc23nC9oQ4FX_k2Cs';

var testingOpenid = 'oNaHXsspWthiBKBda-E5C_OBP_ao';

var user_info = {
  "subscribe": 1,
  "openid": "oNaHXsspWthiBKBda-E5C_OBP_ao",
  "nickname": "曾健",
  "sex": 1,
  "language": "zh_CN",
  "city": "大连",
  "province": "辽宁",
  "country": "中国",
  "headimgurl": "http:\/\/wx.qlogo.cn\/mmopen\/7y4xnsv09YZOoic8yb10tqx5rnflfUEFFbMD44F8yqGrDIcAutL4hucrDLeXCN98QQFPkyuNWgj4CpYMMwsygMnJDuplOjF5u\/0",
  "subscribe_time": 1449009999,
  "remark": "",
  "groupid": 0,
  "certificated": true,
  "email": "neil.zeng@tsinghua.edu.cn"
};

describe('app/components/backend/service/register', function () {
  describe('#unregister().用户取消注册', function () {
    this.timeout(5000);
    // 测试前置处理
    beforeEach(function () {
      weixinDao.add(user_info);
    });

    // 测试后置处理
    afterEach(function () {
      return new Promise(function (resolve, reject) {

        // 清空weixinusers collection
        weixinModel.remove({}, function (weixinErr) {
          if (weixinErr) throw new Error(weixinErr);

          // 清空accessToken collection
          accessTokenModel.remove({}, function (tokenErr) {
            if (tokenErr) throw new Error(tokenErr);
            resolve();
          });
        });
      });
    });

    it('[unregister.subscribe.Nor.01].用户取消关注将认证状态改到初始值', function () {
      return register.unregister({FromUserName: testingOpenid}).then(function (result) {
        assert.equal(false, result.certificated);
        assert.equal(0, result.subscribe);
      });
    });

    it('[unregister.subscribe.Nor.02].未知用户的取消关注,不更改数据,返回错误信息', function () {
      var invalidOpenId = 'no this openid';

      return register.unregister({FromUserName: invalidOpenId}).then(function (result) {

        assert.equal("openID为:" + invalidOpenId + "的用户不存在！", result);
      });
    });

  });

});

